<?php

require_once('Classes/Datasources/Filters/Filter_And.php');
require_once('Classes/Datasources/Filters/Filter_TableStartsWith.php');

Mock::generate( 'Filter_TableStartsWith' );

class Test_Filter_And extends UnitTestCase {
    
    function test_ShouldIncludeTableWillReturnFalseIfTheFirstFilterReturnsTrueAndTheSecondReturnsFalseAskingBothIfTheTableShouldBeIncluded() {

		$sTableName = 'TableThing';

		$oFilterReturnsTrue = &new MockFilter_TableStartsWith();
		$oFilterReturnsTrue->setReturnValue( 'ShouldIncludeTable', true );
		$oFilterReturnsTrue->expectOnce( 'ShouldIncludeTable', array( $sTableName ) );
		
		$oFilterReturnsFalse = &new MockFilter_TableStartsWith();
		$oFilterReturnsFalse->setReturnValue( 'ShouldIncludeTable', false );
		$oFilterReturnsFalse->expectOnce( 'ShouldIncludeTable', array( $sTableName ) );

    	$oAndGate = new Filter_And( $oFilterReturnsTrue, $oFilterReturnsFalse );
        $this->assertFalse(  $oAndGate->ShouldIncludeTable( $sTableName ) );
    }

    function test_ShouldIncludeTableWillReturnFalseIfTheFirstFilterReturnsTrueAndTheSecondReturnsNullAskingBothIfTheTableShouldBeIncluded() {

		$sTableName = 'TableThing';

		$oFilterReturnsTrue = &new MockFilter_TableStartsWith();
		$oFilterReturnsTrue->setReturnValue( 'ShouldIncludeTable', true );
		$oFilterReturnsTrue->expectOnce( 'ShouldIncludeTable', array( $sTableName ) );
		
		$oFilterReturnsNull = &new MockFilter_TableStartsWith();
		$oFilterReturnsNull->setReturnValue( 'ShouldIncludeTable', false );
		$oFilterReturnsNull->expectOnce( 'ShouldIncludeTable', array( $sTableName ) );

    	$oAndGate = new Filter_And( $oFilterReturnsTrue, $oFilterReturnsNull );
        $this->assertFalse(  $oAndGate->ShouldIncludeTable( $sTableName ) );
    }

    function test_ShouldIncludeTableWillReturnFalseIfTheFirstFilterReturnsFalseOnlyAskingTheFirstFilterIfTheTableShouldBeIncluded() {

		$sTableName = 'TableThing';

		$oFilterReturnsFalse = &new MockFilter_TableStartsWith();
		$oFilterReturnsFalse->setReturnValue( 'ShouldIncludeTable', false );
		$oFilterReturnsFalse->expectOnce( 'ShouldIncludeTable', array( $sTableName ) );

		$oFilterReturnsTrue = &new MockFilter_TableStartsWith();
		$oFilterReturnsTrue->setReturnValue( 'ShouldIncludeTable', true );
		$oFilterReturnsTrue->expectNever( 'ShouldIncludeTable' );
		
    	$oAndGate = new Filter_And( $oFilterReturnsFalse, $oFilterReturnsTrue );
        $this->assertFalse(  $oAndGate->ShouldIncludeTable( $sTableName ) );
    }

    function test_ShouldIncludeTableWillReturnFalseIfTheFirstFilterReturnsNullOnlyAskingTheFirstFilterIfTheTableShouldBeIncluded() {

		$sTableName = 'TableThing';

		$oFilterReturnsNull = &new MockFilter_TableStartsWith();
		$oFilterReturnsNull->setReturnValue( 'ShouldIncludeTable', null );
		$oFilterReturnsNull->expectOnce( 'ShouldIncludeTable', array( $sTableName ) );

		$oFilterReturnsTrue = &new MockFilter_TableStartsWith();
		$oFilterReturnsTrue->setReturnValue( 'ShouldIncludeTable', true );
		$oFilterReturnsTrue->expectNever( 'ShouldIncludeTable' );
		
    	$oAndGate = new Filter_And( $oFilterReturnsNull, $oFilterReturnsTrue );
        $this->assertFalse(  $oAndGate->ShouldIncludeTable( $sTableName ) );
    }

    function test_ShouldIncludeTableWillReturnTrueIfTheBothTheFiltersReturnTrueAskingBothTheFiltersIfTheTableShouldBeIncluded() {

		$sTableName = 'TableThing';

		$oFilterReturnsTrue = &new MockFilter_TableStartsWith();
		$oFilterReturnsTrue->setReturnValue( 'ShouldIncludeTable', true );
		$oFilterReturnsTrue->expectCallCount( 'ShouldIncludeTable', 2 );
		
    	$oAndGate = new Filter_And( $oFilterReturnsTrue, $oFilterReturnsTrue );
        $this->assertTrue(  $oAndGate->ShouldIncludeTable( $sTableName ) );
    }
}

?>
